# ###############################################################################
# Revisions
# Rev 1.0.1.0
# Changes: Pointed the GEORSSPATH to new site. Nautilus was having various issues
# causing the georss feeds to lag.
# ###############################################################################
from pysqlite2 import dbapi2 as sqlite
import re, sys 

GEORSSPATH = 'http://129.252.37.90/xenia/feeds/georss/'
DATAQUERYPAGEPATH = 'http://carolinasrcoos.org/queryStation.php?station='
ADCPGRAPHURL = 'http://carocoops.org/~dramage_prod/cgi-bin/rcoos/ADCPGraph.php?PLATFORMID=<ID>&INTERVAL=<INTERVAL>'

class HTMLObsDB:
    def __init__ ( self, strDBFileName ):
        try:
            self.DB = sqlite.connect( strDBFileName )
            self.strHTML = ''
            self.strCurrentPlatform = ''                        
        except Exception, E:
            print( 'ERROR: ' + str(E) )
            
    def GetPlatformHandles(self, aPlatforms ):
        try:
            DBCursor = self.DB.cursor()
            strSQL = "SELECT platform_handle FROM html_content"
            DBCursor.execute( strSQL )
            iRowCnt = 0;
            for row in DBCursor:
                aPlatforms.append( row[0] )
                iRowCnt += 1
                
        except Exception, E:
            print( 'ERROR: ' + str(E) )
        return( iRowCnt )
    #Get the HTML column data for the given platform. This sets the platform and html we will work on until this
    #function is called again.
    def GetHTML(self, strPlatformHandle ):
        try:
            self.strCurrentPlatform = strPlatformHandle
            DBCursor = self.DB.cursor()
            #Get the html for the given platform so we can make the modification.
            strSQL = "SELECT html FROM html_content WHERE platform_handle = '%s'" % strPlatformHandle
            DBCursor.execute( strSQL )
            for row in DBCursor:
                self.strHTML = row[0]
        except Exception, E:
            print( 'ERROR: ' + str(E) )
            sys.exit(0)
        
    def AddToObsHeader(self, strPlatformHandle, strData ):
            #The <!-- DAN_HEADER --> in the data is our tag to put in whatever we want in the header.            
            self.strHTML = self.strHTML.replace( '<!-- DAN_HEADER -->', strData + ' <!-- DAN_HEADER -->' )
            
    def AddToObsFooter(self, strPlatformHandle, strData ):
            #The <!-- DAN_FOOTER --> in the data is our tag to put in whatever we want in the footer.            
        self.strHTML = self.strHTML.replace( '<!-- DAN_FOOTER -->', strData + ' <!-- DAN_FOOTER -->' )

    def UpdateHTML(self ):
        try:
            DBUpdateCursor = self.DB.cursor()
            strSQL = "UPDATE html_content SET html = '%s' WHERE platform_handle = '%s'" % (self.strHTML, self.strCurrentPlatform) 
            DBUpdateCursor.execute( strSQL )
            self.DB.commit()
            self.strHTML = ''
            self.strCurrentPlatform = ''                        
        except Exception, E:
            print( 'ERROR: ' + str(E) )
            sys.exit(0)
        
    

if __name__ == '__main__':
    try:    
        if( len( sys.argv ) < 2 ):
            print( 'Usage: EmbedLinks.py HtmlObsTablePath\n' )
            sys.exit(0)
        strDB = sys.argv[1]        
        HTMLObs = HTMLObsDB( strDB )
        aPlatforms = [];
        HTMLObs.GetPlatformHandles( aPlatforms )
        #Add the links to the header
        for strPlatform in aPlatforms:
            HTMLObs.GetHTML( strPlatform )
            #Add GeoRSS link
            strPlat = strPlatform.replace( '.', '_' )
            strBuf = '<br/><a href=' + GEORSSPATH + strPlat + '_GeoRSS_latest.xml target=new><img src="images/rss_small.jpg"/></a>'
            HTMLObs.AddToObsHeader( strPlatform, strBuf )

            #If the platform is an ADCP, we want to add the plotting link.
            if( strPlatform.find( 'adcp' ) != -1 ):
              #strBuf = '<br/><a href=' + ADCPGRAPHURL + strPlatform + ' target=\"new\">ADCP Graph</a>'
              strURL = ADCPGRAPHURL
              strURL = strURL.replace( '<ID>', strPlatform, 1 )
              strURL = strURL.replace( '<INTERVAL>', '24', 1 )              
              strBuf = '<a href=' + strURL + ' target=\"new\">24hrs</a> '
              strURL = ADCPGRAPHURL
              strURL = strURL.replace( '<ID>', strPlatform, 1 )
              strURL = strURL.replace( '<INTERVAL>', '48', 1 )              
              strBuf = strBuf + '<a href=' + strURL + ' target=\"new\">48hrs</a> '
              strURL = ADCPGRAPHURL
              strURL = strURL.replace( '<ID>', strPlatform, 1 )
              strURL = strURL.replace( '<INTERVAL>', '72', 1 )              
              strBuf = strBuf + '<a href=' + strURL + ' target=\"new\">72hrs</a>'
              strBuf = '<br/>ADCP Graph Last: ' + strBuf
              HTMLObs.AddToObsFooter( strPlatform, strBuf )

            #Get the station name, the format is organization.name.type, so we plit based on the '.'.
            aPlatPieces = strPlatform.split('.')
            strBuf = '<br/><a href=' + DATAQUERYPAGEPATH + aPlatPieces[1].upper() + ' target=new>Data Query</a>'
            HTMLObs.AddToObsFooter( strPlatform, strBuf )
            HTMLObs.UpdateHTML()
    except Exception, E:
        print( 'ERROR: ' + str(E) )